/*
 * Copyright 2019 The ZuSmart Project
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zusmart.base.logging.support;

import static org.slf4j.spi.LocationAwareLogger.DEBUG_INT;
import static org.slf4j.spi.LocationAwareLogger.ERROR_INT;
import static org.slf4j.spi.LocationAwareLogger.INFO_INT;
import static org.slf4j.spi.LocationAwareLogger.TRACE_INT;
import static org.slf4j.spi.LocationAwareLogger.WARN_INT;

import org.slf4j.spi.LocationAwareLogger;

/**
 * @author Administrator
 *
 */
public class Slf4JLoggerAware extends AbstractLogger {

	private static final long serialVersionUID = 2559564669604819459L;

	private static final String FQCN = Slf4JLoggerAware.class.getName();
	private final transient LocationAwareLogger logger;

	public Slf4JLoggerAware(LocationAwareLogger logger) {
		super(logger.getName());
		this.logger = logger;
	}

	private void log(final int level, final String message) {
		this.logger.log(null, FQCN, level, message, null, null);
	}

	private void log(final int level, final String message, Throwable cause) {
		this.logger.log(null, FQCN, level, message, null, cause);
	}

	private void log(final int level, final org.slf4j.helpers.FormattingTuple tuple) {
		this.logger.log(null, FQCN, level, tuple.getMessage(), tuple.getArgArray(), tuple.getThrowable());
	}

	@Override
	public boolean isTraceEnabled() {
		return this.logger.isTraceEnabled();
	}

	@Override
	public boolean isDebugEnabled() {
		return this.logger.isDebugEnabled();
	}

	@Override
	public boolean isInfoEnabled() {
		return this.logger.isInfoEnabled();
	}

	@Override
	public boolean isWarnEnabled() {
		return this.logger.isWarnEnabled();
	}

	@Override
	public boolean isErrorEnabled() {
		return this.logger.isErrorEnabled();
	}

	@Override
	public void trace(String msg) {
		if (this.isTraceEnabled()) {
			this.log(TRACE_INT, msg);
		}
	}

	@Override
	public void trace(String format, Object arg) {
		if (this.isTraceEnabled()) {
			this.log(TRACE_INT, org.slf4j.helpers.MessageFormatter.format(format, arg));
		}
	}

	@Override
	public void trace(String format, Object argA, Object argB) {
		if (this.isTraceEnabled()) {
			this.log(TRACE_INT, org.slf4j.helpers.MessageFormatter.format(format, argA, argB));
		}
	}

	@Override
	public void trace(String format, Object... argArray) {
		if (this.isTraceEnabled()) {
			this.log(TRACE_INT, org.slf4j.helpers.MessageFormatter.arrayFormat(format, argArray));
		}
	}

	@Override
	public void trace(String msg, Throwable t) {
		if (this.isTraceEnabled()) {
			this.log(TRACE_INT, msg, t);
		}
	}

	@Override
	public void debug(String msg) {
		if (this.isDebugEnabled()) {
			this.log(DEBUG_INT, msg);
		}
	}

	@Override
	public void debug(String format, Object arg) {
		if (this.isDebugEnabled()) {
			this.log(DEBUG_INT, org.slf4j.helpers.MessageFormatter.format(format, arg));
		}
	}

	@Override
	public void debug(String format, Object argA, Object argB) {
		if (this.isDebugEnabled()) {
			this.log(DEBUG_INT, org.slf4j.helpers.MessageFormatter.format(format, argA, argB));
		}
	}

	@Override
	public void debug(String format, Object... argArray) {
		if (this.isDebugEnabled()) {
			this.log(DEBUG_INT, org.slf4j.helpers.MessageFormatter.arrayFormat(format, argArray));
		}
	}

	@Override
	public void debug(String msg, Throwable t) {
		if (this.isDebugEnabled()) {
			this.log(DEBUG_INT, msg, t);
		}
	}

	@Override
	public void info(String msg) {
		if (this.isInfoEnabled()) {
			this.log(INFO_INT, msg);
		}
	}

	@Override
	public void info(String format, Object arg) {
		if (this.isInfoEnabled()) {
			this.log(INFO_INT, org.slf4j.helpers.MessageFormatter.format(format, arg));
		}
	}

	@Override
	public void info(String format, Object argA, Object argB) {
		if (this.isInfoEnabled()) {
			this.log(INFO_INT, org.slf4j.helpers.MessageFormatter.format(format, argA, argB));
		}
	}

	@Override
	public void info(String format, Object... argArray) {
		if (this.isInfoEnabled()) {
			this.log(INFO_INT, org.slf4j.helpers.MessageFormatter.arrayFormat(format, argArray));
		}
	}

	@Override
	public void info(String msg, Throwable t) {
		if (this.isInfoEnabled()) {
			this.log(INFO_INT, msg, t);
		}
	}

	@Override
	public void warn(String msg) {
		if (this.isWarnEnabled()) {
			this.log(WARN_INT, msg);
		}
	}

	@Override
	public void warn(String format, Object arg) {
		if (this.isWarnEnabled()) {
			this.log(WARN_INT, org.slf4j.helpers.MessageFormatter.format(format, arg));
		}
	}

	@Override
	public void warn(String format, Object... argArray) {
		if (this.isWarnEnabled()) {
			this.log(WARN_INT, org.slf4j.helpers.MessageFormatter.arrayFormat(format, argArray));
		}
	}

	@Override
	public void warn(String format, Object argA, Object argB) {
		if (this.isWarnEnabled()) {
			this.log(WARN_INT, org.slf4j.helpers.MessageFormatter.format(format, argA, argB));
		}
	}

	@Override
	public void warn(String msg, Throwable t) {
		if (this.isWarnEnabled()) {
			this.log(WARN_INT, msg, t);
		}
	}

	@Override
	public void error(String msg) {
		if (this.isErrorEnabled()) {
			this.log(ERROR_INT, msg);
		}
	}

	@Override
	public void error(String format, Object arg) {
		if (this.isErrorEnabled()) {
			this.log(ERROR_INT, org.slf4j.helpers.MessageFormatter.format(format, arg));
		}
	}

	@Override
	public void error(String format, Object argA, Object argB) {
		if (this.isErrorEnabled()) {
			this.log(ERROR_INT, org.slf4j.helpers.MessageFormatter.format(format, argA, argB));
		}
	}

	@Override
	public void error(String format, Object... argArray) {
		if (this.isErrorEnabled()) {
			this.log(ERROR_INT, org.slf4j.helpers.MessageFormatter.arrayFormat(format, argArray));
		}
	}

	@Override
	public void error(String msg, Throwable t) {
		if (this.isErrorEnabled()) {
			this.log(ERROR_INT, msg, t);
		}
	}

}